angular.module('Controller')

.controller 'WFLoginCtrl', ($scope, $state,cryptUtil,commonUtil,userData,$localStorage,wfAlert,$http,urls) ->
    $scope.$storage = $localStorage
    if $localStorage.remember == undefined
        $localStorage.remember = false
    _.extend $scope,
        login:->
            data={
                userName:$localStorage.login.data?.username
                passWord:$localStorage.login.data?.password
            }
            $http.get('@sunagy/Ui/ApiSunagy/loginToken',params:data)
            .success((resp)->
                if resp.code=='000_000'
                    tmp = false
                    if $localStorage.remember
                        tmp = true
                    else
                        tmp = false
                    $localStorage.$reset()
                    $localStorage.firstMenuShow = true
                    $localStorage.remember = tmp
                    if $localStorage.remember
                        $localStorage.login = {data:{
                            username:data.userName
                            password:data.passWord
                        }}
                    else
                        $localStorage.login = {data:{
                            username:''
                            password:''
                        }}
                    userData.setUser resp.data.user
                    userData.setToken resp.data.token
                    window.localStorage.token = resp.data.token
                    $localStorage.menuList = JSON.parse(resp.data.menuList)
                    exec_obj = document.createElement('iframe');
                    exec_obj.style.display = 'none';
                    url = window.location.href.split('#')
                    encodeUrl = encodeURIComponent("#{url[0]}logout.html");
                    exec_obj.src = "#{urls.sys_backend}#{resp.data.url}?token=#{resp.data.token}&forward=#{encodeUrl}"
                    document.body.appendChild(exec_obj);
                    exec_obj.onload = ->
                        $state.go 'main'
                else
                    wfAlert.toastError("请检查用户名和密码")
                    if $localStorage.remember
                        $localStorage.login = {data:{
                            username:data.userName
                            password:data.passWord
                        }}
                    else
                        $localStorage.login = {data:{
                            username:''
                            password:''
                        }}
            )

